﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using ArtMath.Resource;
using ArtMath.Core.Helper;

namespace ArtMath.Control
{
    public partial class AngleControl : UserControl
    {
        const int VerticalOffset = 20;
        double defaultAngle, oldAngle, newAngle;
        bool cancel;
        public event Action<double> RefreshObject;
        public AngleControl(double angle, double defaultAngle = 0)
        {
            InitializeComponent();
            oldAngle = newAngle = angle;
            this.defaultAngle = defaultAngle;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            UpdateStyles();
        }
        public double NewAngle
        {
            get { return newAngle; }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                oldAngle = newAngle;
                cancel = true;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void AngleControl_Leave(object sender, EventArgs e)
        {
            if (!cancel)
            {
                newAngle = oldAngle;
            }
        }

        private void AngleControl_Paint(object sender, PaintEventArgs e)
        {
            Graphics gra = e.Graphics;
            gra.SmoothingMode = SmoothingMode.HighQuality;
            Font ft = new Font("Microsoft YaHei", 9);
            double oldDeg = oldAngle * Angle.RToD;
            gra.DrawString($"{Resources.Translate("Degree")}{Resources.ColonSeparate}{oldDeg}°{Environment.NewLine}{Resources.Translate("Radian")}{Resources.ColonSeparate}{oldAngle}", ft, Brushes.Black, 6, 6);
            gra.DrawString(Resources.Translate("RMB0"), ft, Brushes.Red, 6, Height - 20);
            gra.TranslateTransform(Width >> 1, Height / 2 + 10);
            int r = Math.Min(Width, Height) / 2 - VerticalOffset;
            Pen p = new Pen(Color.DimGray, 5);
            gra.DrawEllipse(p, -r, -r, r * 2, r * 2);//画最外层圆
            p.Width = 2;
            p.DashStyle = DashStyle.Dash;
            gra.DrawLine(p, 0, 0, r - 2, 0);//画初始半径
            p.DashStyle = DashStyle.Solid;
            gra.DrawArc(p, -VerticalOffset, -VerticalOffset, VerticalOffset * 2, VerticalOffset * 2, 0, (float)-oldDeg);//画角标
            gra.RotateTransform((float)-oldDeg);
            p = new Pen(Color.Black, 3) { CustomEndCap = new AdjustableArrowCap(4, 4, true) };
            gra.DrawLine(p, 0, 0, r - 2, 0);//画指针
            gra.RotateTransform((float)oldDeg);
            gra.DrawEllipse(Pens.Black, -4, -4, 8, 8);
            gra.FillEllipse(Brushes.Black, -4, -4, 8, 8);//画中心圆点
        }

        private void AngleControl_MouseDown(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    AngleControl_MouseMove(sender, e);
                    break;
                case MouseButtons.Right:
                    oldAngle = defaultAngle;
                    Invalidate();
                    refresh();
                    break;
            }
        }

        private void AngleControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                oldAngle = (Angle.RadRound - Math.Atan2(e.Y - Height / 2 - 15, e.X - Width / 2)) % Angle.RadRound;
                Invalidate();
                refresh();
            }
        }
        void refresh()
        {
            RefreshObject?.Invoke(oldAngle);
        }
    }
}
